package com.sense.kukoo.biz_media.adapter.provider

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.sense.kukoo.biz_media.R
import com.sense.kukoo.biz_media.adapter.CommentContentAdapter
import com.sense.kukoo.common.repo.biz.CommentContentBean
import com.sense.kukoo.common.repo.http.bean.CommentRecordBean
import com.sense.kukoo.common.utils.DataTimeUtil
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class CommentLevel2ItemProvider(val listener: CommentContentAdapter.CommentListener): MultiItemProvider<CommentContentBean>() {

    private var creatorId: Long = -1
    private var currentUserId: Long = -1

    override fun getItemType(): Int = CommentContentBean.TYPE_COMMENT_LEVEL_2

    override fun getItemLayoutId(): Int = R.layout.biz_media_item_comment_level_2

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: CommentContentBean) {
        val data: CommentRecordBean = item.data!!
        val parent: CommentRecordBean = item.parent!!
        //
        val cl_comment_root = viewHolder.getView(R.id.cl_comment_root)
        cl_comment_root.setOnClickListener {
            listener.onReplyComment(parent, data)
        }
        //
        val iv_btn_more = viewHolder.getView(R.id.iv_btn_more)
        val click_btn_more = viewHolder.getView(R.id.click_btn_more)
        var isShowMoreBtn = false
        if(currentUserId == creatorId){
            isShowMoreBtn = true
        } else if(currentUserId == data.userId){
            isShowMoreBtn = true
        }
        if (isShowMoreBtn) {
            iv_btn_more.visibility = View.VISIBLE
            click_btn_more.visibility = View.VISIBLE
            click_btn_more.setOnClickListener {
                listener.onBtnMoreClick(iv_btn_more, parent, data)
            }
        } else {
            iv_btn_more.visibility = View.GONE
            click_btn_more.visibility = View.GONE
        }
        //
        val iv_icon_item = viewHolder.getView(R.id.iv_icon_item) as ImageView
        Monet.get(iv_icon_item.context)
            .load<GlideConfig>(data.avatarUrl)
            .placeholder(com.sense.kukoo.common_design.R.drawable.design_icon_avatar_default)
            .circleCrop()
            .into(iv_icon_item)
        //
        val tv_name_channel = viewHolder.getView(R.id.tv_name_channel) as TextView
        tv_name_channel.text = data.channelName
        //
        val iv_name_channel_tag = viewHolder.getView(R.id.iv_name_channel_tag) as ImageView
        if(creatorId == data.userId){
            iv_name_channel_tag.visibility = View.VISIBLE
        } else {
            iv_name_channel_tag.visibility = View.GONE
        }
        //
        val tv_name_channel_at = viewHolder.getView(R.id.tv_name_channel_at) as TextView
        data.replyUserName?.let {
            tv_name_channel_at.text = "@${it}"
        }
        //
        val tv_time_ago = viewHolder.getView(R.id.tv_time_ago) as TextView
        tv_time_ago.text = DataTimeUtil.dateTransform1(data.createdAt)
        //
        val tv_text_comment = viewHolder.getView(R.id.tv_text_comment) as TextView
        tv_text_comment.text = data.content
    }

    fun setCreatorId(creatorId: Long, currentUserId: Long){
        this.creatorId = creatorId
        this.currentUserId = currentUserId
    }
}